﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 各方面的碰撞体生成器.
/// </summary>
public class ColliderGenerator : MonoBehaviour
{

    private readonly float _edge = 10.0f;

    private BoxCollider _boxCollider;

    private void Awake()
    {
        _boxCollider = GetComponent<BoxCollider>();
    }

    private void Start()
    {
        GenerateColl();
    }

    [SerializeField]
    private bool _left;
    [SerializeField]
    private bool _right;
    [SerializeField]
    private bool _front;
    [SerializeField]
    private bool _back;


    private void GenerateColl()
    {
        if (_left)
        {
            GenPerColl("left");
        }
        if (_right)
        {
            GenPerColl("right");
        }
        if (_front)
        {
            GenPerColl("front");
        }
        if (_back)
        {
            GenPerColl("back");
        }
        
    }

    /// <summary>
    /// 生成一个边界碰撞体.
    /// </summary>
    /// <param name="name"></param>
    private void GenPerColl(string name)
    {
        GameObject go = new GameObject(name);
        //go.transform.position = transform.position;
        switch (name)
        {
            case "left":
                go.transform.position = new Vector3(-_edge, transform.position.y, transform.position.z);
                break;
            case "right":
                go.transform.position = new Vector3(_edge, transform.position.y, transform.position.z);
                break;
            case "front":
                go.transform.position = new Vector3(transform.position.x, transform.position.y, _edge);
                break;
            case "back":
                go.transform.position = new Vector3(transform.position.x, transform.position.y, -_edge);
                break;
            default:
                break;
        }
        go.transform.rotation = transform.rotation;
        go.transform.localScale = transform.localScale;
        var coll = go.AddComponent<BoxCollider>();
        //TODO:调整size.
        switch (name)
        {
            case "left":
            case "right":
                coll.center = new Vector3(_boxCollider.center.x, _boxCollider.center.y, 0);
                break;
            case "front":
            case "back":
                coll.center = new Vector3(0, _boxCollider.center.y, _boxCollider.center.z);
                break;
            default:
                break;
        }
        coll.size = _boxCollider.size;
        
        go.transform.SetParent(transform);
    }
}
